# -*- utf-8 -*-

import requests
import json
import re
from time import sleep
from bs4 import BeautifulSoup
import time
import datetime
from requests.adapters import HTTPAdapter
from time import strftime, localtime


def baojing(wenben):
    dingding = '钉钉报警接口'

    headers = {
        'Content-Type': 'application/json'
    }
    aaa = {"msgtype": "text",
            "text": {
                "content": wenben
            }}
    resp = requests.post(url=dingding, headers=headers, json=aaa)
    print(resp)

def get_bs(link):
    s = requests.Session()
    s.mount('http://', HTTPAdapter(max_retries=3))  # 设置重试次数为3次
    s.mount('https://', HTTPAdapter(max_retries=3))
    response = s.get(link, timeout=150)
    ttt = response.content.decode('utf-8')
    soup = BeautifulSoup(ttt, 'lxml')
    p = soup.find_all('p')
    p1 = str(p)

    f1 = open('已报警内容.txt', 'r+', encoding='utf-8')
    list1 = f1.readlines()

    if '抱歉，未找到' in p1:
        print(' ')
    else:
        print('微博当日舆论：')
        for i in p:
            i = str(i)
            if 'class="s-color-red"' in i and '展开全文' not in i:
                ii = i.split('nick-name=')[1].split('</p>')[0].strip()
                iiii = re.sub('node.*\\s', ': ', ii)
                iii = re.sub('<(S*?)[^>]*>.*?|<.*? />', '', iiii).replace(' ', '')   # 去除html字符且去除空格
                print(iii+'\n')
                if '骗人' in iii or '退' in iii or '欺' in iii or '12315' in iii or '消费' in iii or '投诉' in iii or '售后' in iii or '客服' in iii or '共产党' in iii or '辱' in iii or '港' in iii or '曝光' in iii or '暴光' in iii:
                    neir = '微博舆论报警：' + '\t' + iii
                    if neir in list1:
                        print('■■■■■■■■■■■■■已经报警过了')
                    else:
                        lianjie = str(link)
                        baojing('链接:'+'\t'+lianjie)
                        baojing(neir)
                        f1.writelines(neir)

                else:
                    continue

            else:
                continue
    f1.close()


#f = open('已报警内容.txt', 'w', encoding='utf-8')

while 1:
    now = datetime.datetime.now()
    otherStyleTime = now.strftime("%Y-%m-%d")

    url = 'https://s.weibo.com/weibo?q=%E7%9F%B3%E5%8A%9B%E6%B4%BE&typeall=1&suball=1&timescope=custom:{}-0:{}-23&Refer=SWeibo_box'.format(
        otherStyleTime, otherStyleTime)  # 以石力派为关键词搜索微博
    url1 = 'https://s.weibo.com/weibo?q=%E7%9F%B3%E5%8A%9B%E6%B4%BE%E7%BF%A1%E7%BF%A0&typeall=1&suball=1&timescope=custom:{}-0:{}-23&Refer=SWeibo_box'.format(
        otherStyleTime, otherStyleTime)  # 以石力派翡翠为关键词搜索微博
    url2 = 'https://s.weibo.com/weibo?q=%E7%9F%B3%E5%8A%9B%E6%B4%BE%E7%9B%B4%E6%92%AD&typeall=1&suball=1&timescope=custom:{}-0:{}-23&Refer=SWeibo_box'.format(
        otherStyleTime, otherStyleTime)  # 以石力派直播为关键词搜索微博

    try:

        page = get_bs(url)
        print('以石力派为关键词搜索微博完毕...')
        time.sleep(5)

        page1 = get_bs(url1)
        print('以石力派翡翠为关键词搜索微博完毕....')
        time.sleep(6)

        page2 = get_bs(url2)
        print('以石力派直播为关键词搜索微博完毕，半小时后进入下一轮搜索')

        print(strftime('%Y-%m-%d %H:%M:%S', localtime()))
        time.sleep(1800)
    except Exception as e:
        print(e)
        time.sleep(1800)
    continue





